package com.example.threadpool2.config;

import java.util.concurrent.*;

/**
 * 1
 *
 * @author zhuyonggang
 * @DateTime: 2021/8/30 15:47
 */
public class test {
    static int count = 100;
    public static void main(String[] args) {
//        long millis = System.currentTimeMillis();
//        test();
//        long millis2 = System.currentTimeMillis();
//        System.out.println("耗时:" + (millis2-millis)/1000);

//        long millis3 = System.currentTimeMillis();
//        test2();
//        long millis4 = System.currentTimeMillis();
//        System.out.println("多线程耗时:" + (millis4-millis3)/1000);

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                5,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>(10)
        );
        long millis5 = System.currentTimeMillis();
        try {
            test3(threadPoolExecutor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long millis6 = System.currentTimeMillis();
        System.out.println("-----------------------------------线程池耗时:" + (millis6-millis5)/1000);
    }

    private static void test3(ThreadPoolExecutor threadPoolExecutor) {
        for (int i = 0; i < 10; i++) {
            int[] ints = {i,i+1,i+3,i+4,i+5,i+6,i+7,i+8,i+9};
//            MyTask task = new MyTask("测试", ints);
            MyTask task1 = new MyTask("测试");
            task1.setInts(ints);
            threadPoolExecutor.execute(task1);
        }

    }

    private static void test2() {
        for (int i = 0; i < count; i++) {
            MyTask myTask = new MyTask("测试", new int[1]);
            Thread thread = new Thread(myTask);
            thread.start();
        }
    }

    private static void test() {
        for (int i = 0; i < count; i++) {
            sleep();
        }
    }

    public static void sleep(){
        try {
            Thread.sleep(1000);
            System.out.println("Thread:"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
